package com.cn.mmd.controller;

import com.cn.mmd.model.LogManage;
import com.cn.mmd.model.TGroup;
import com.cn.mmd.model.TeachingGroup;
import com.cn.mmd.service.LogManageService;
import com.cn.mmd.service.TGroupLService;
import com.cn.mmd.service.TeachingGrupServiceL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 教学组操作
 * Created by Administrator on 2016/8/4 0004.
 */

@Controller
@RequestMapping(value = "page/manageTeaGroup")
public class ManageTeaGroupController {

    @Autowired
    private TGroupLService tGroupLService;

    @Autowired
    private TeachingGrupServiceL teachingGrupServiceL;

    @Autowired
    private LogManageService logManageService;

    /**
     * 添加一个教学组
     * @param request
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody public synchronized  Object  manageTeaGroupAdd(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String teachingGroup = request.getParameter("teachingGroupName");
        TGroup tGroup = new TGroup();
        tGroup.setTeachinggroupname(teachingGroup);
        if (tGroupLService.insertSelective(tGroup)>0){
            objectMap.put("code",200);
            objectMap.put("message","成功");
            Date date = new Date();
            SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime=time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("添加了一个课程组");
            logManageService.insertSelective(logManage);
        }else {
            objectMap.put("code",201);
            objectMap.put("message","失败");
        }
        return objectMap;
    }

    /**
     * 删除一个教学组
     * @param request
     * @return
     */
    @RequestMapping(value = "/del")
    @ResponseBody public synchronized  Object manageTeaGroupDel(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        Integer teaGroupId = Integer.parseInt(request.getParameter("teaGroupId"));
        if (tGroupLService.deleteByPrimaryKey(teaGroupId)>0){
            teachingGrupServiceL.deleteByGroupId(teaGroupId);
            objectMap.put("code",200);
            objectMap.put("message","成功");
            Date date = new Date();
            SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime=time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("删除了一个课程组:"+teaGroupId);
            logManageService.insertSelective(logManage);
        }else {
            objectMap.put("code",201);
            objectMap.put("message","失败");
        }
        return objectMap;
    }

    /**
     * 修改教学组组名
     * @param request
     * @return
     */
    @RequestMapping(value = "/upd")
    @ResponseBody public synchronized  Object manageTeaGroupUpd(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        Integer teaGroupId = Integer.parseInt(request.getParameter("teaGroupId"));
        String teachingGroup = request.getParameter("teachingGroupName");
        TGroup tGroup = new TGroup();
        tGroup.setTeachinggroupid(teaGroupId);
        tGroup.setTeachinggroupname(teachingGroup);
        if (tGroupLService.updateByPrimaryKeySelective(tGroup)>0){
            objectMap.put("code",200);
            objectMap.put("message","成功");
            Date date = new Date();
            SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime=time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("修改了一个课程组名:"+teaGroupId);
            logManageService.insertSelective(logManage);
        }else {
            objectMap.put("code",201);
            objectMap.put("message","失败");
        }
        return objectMap;
    }

    /**
     * 根据教学组名查询
     * @param request
     * @return
     */
    @RequestMapping(value = "/select")
    @ResponseBody public synchronized  Object manageTeaGroupSel(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        String manageId = request.getParameter("manageId");
        Integer startrow = Integer.parseInt(request.getParameter("startrow"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String teachingGroupName = request.getParameter("teachingGroupName");
        TGroup tGroup = new TGroup();
        tGroup.setStartrow(startrow);
        tGroup.setPageSize(pageSize);
        tGroup.setTeachinggroupname(teachingGroupName);
        List<TGroup> tGroupList = tGroupLService.selectByPaging(tGroup);
        if (tGroupList!=null&&tGroupList.size()>0){
            List<TeachingGroup> teachingGroupList;
            TeachingGroup teachingGroup;
            Map<String,Object> map;
            for (int i = 0 ; i < tGroupList.size(); i ++){
                tGroup = tGroupList.get(i);
                teachingGroupList = teachingGrupServiceL.selectByTeachingGroupId(tGroup.getTeachinggroupid());
                if (teachingGroupList!=null&&teachingGroupList.size()>0){
                    for (int i1 = 0 ; i1 < teachingGroupList.size(); i1++){
                        map = new HashMap<String, Object>();
                        map.put("teachingGroupId", tGroup.getTeachinggroupid());
                        map.put("teachingGroupMame", tGroup.getTeachinggroupname());
                        teachingGroup = teachingGroupList.get(i1);
                        if (teachingGroup.getTeachinggroupmanageid()!=null&&!teachingGroup.getTeachinggroupmanageid().equals("")) {
                            map.put("teachingGroupManageId", teachingGroup.getTeachinggroupmanageid());
                        }else {
                            map.put("teachingGroupManageId","暂无管理");
                        }
                        mapList.add(map);
                    }
                }else {
                    map = new HashMap<String, Object>();
                    map.put("teachingGroupId", tGroup.getTeachinggroupid());
                    map.put("teachingGroupMame", tGroup.getTeachinggroupname());
                    map.put("teachingGroupManageId","暂无管理");
                    mapList.add(map);
                }
            }
            if (mapList!=null){
                TGroup tGroup1 = new TGroup();
                List<TGroup> tGroupList1 = tGroupLService.selectByPaging(tGroup1);
                objectMap.put("code",200);
                objectMap.put("message","成功");
                objectMap.put("data",mapList);
                objectMap.put("total",tGroupList1.size());
            }else {
                objectMap.put("code",201);
                objectMap.put("message","失败");
            }
        }else {
            objectMap.put("code",202);
            objectMap.put("message","没有教师组数据");
        }
        return objectMap;
    }
}
